﻿@namespace Masa.Blazor
@using Masa.Blazor.Components.ItemGroup
@inherits MRoutableGroupItem<MItemGroupBase>

<CascadingValue Value="ComputedTheme" Name="MasaBlazorCascadingTheme">
    <MElement Class="@GetClass()"
              Style="@GetStyle()"
              Tag="@Tag"
              ReferenceCaptureAction="r => Ref = r"
              id="@Id"
              @onclick="HandleOnClick"
              __internal_stopPropagation_onclick="@OnClickStopPropagation"
              __internal_preventDefault_onclick="@OnClickPreventDefault"
              @attributes="@Attributes">
        @if (ItemContent != null)
        {
            @ItemContent?.Invoke(GenItemContext())
        }
        else
        {
            @GenAvatarCore(PrependAvatar)
            @GenIcon(PrependIcon)
            @GenContent()
            @GenIcon(AppendIcon)
            @GenAvatarCore(AppendAvatar)
            @ChildContent
        }
    </MElement>
</CascadingValue>

@code {

    RenderFragment GenIcon(string? icon) => __builder =>
    {
        @if (!string.IsNullOrWhiteSpace(icon))
        {
            <MListItemIcon>
                <MIcon>@icon</MIcon>
            </MListItemIcon>
        }
    };

    private RenderFragment GenAvatarCore(string? src) => __builder =>
    {
        @if (!string.IsNullOrWhiteSpace(src))
        {
            <MListItemAvatar Size="null">
                <MImage Src="@src"></MImage>
            </MListItemAvatar>
        }
    };

    RenderFragment GenContent() => __builder =>
    {
        var hasTitle = !string.IsNullOrWhiteSpace(Title);
        var hasSubtitle = !string.IsNullOrWhiteSpace(Subtitle);

        if (hasTitle || hasSubtitle)
        {
            <MListItemContent>
                @if (hasTitle)
                {
                    <MListItemTitle>
                        @Title
                    </MListItemTitle>
                }

                @if (hasSubtitle)
                {
                    <MListItemSubtitle>
                        @Subtitle
                    </MListItemSubtitle>
                }
            </MListItemContent>
        }
    };

}